package com.king01.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.king01.dao.DeptDao;
import com.king01.entity.Dept;
import com.king01.entity.Staff;
import com.king01.service.DeptService;
import com.king01.vo.DeptVo;
import com.king01.vo.PageBean;
import com.king01.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private HttpSession session;

    @Override
    public Result findAllDept(Integer current, Integer pageSize) {
        PageHelper.startPage(current, pageSize);
        List<Dept> dept = deptDao.findAllDept();
        PageInfo<Dept> pageInfo = new PageInfo<>(dept);
        return new Result(200, "查询成功", pageInfo);
    }

    @Override
    public Result add(Dept dept) {
        Integer integer = deptDao.add(dept);
        return new Result(200, "添加成功", integer);
    }

    @Override
    public Result del(Integer deptid) {
        Integer integer = deptDao.del(deptid);
        return new Result(200, "删除成功", integer);
    }

    @Override
    public Result update1(DeptVo deptVo) {
        Integer integer = deptDao.update1(deptVo);
        return new Result(200, "修改成功", integer);
    }

    @Override
    public Result selByDeptId(Integer page, Integer pageSize, Integer deptid) {
        Staff staff = (Staff) session.getAttribute("staff");
        PageHelper.startPage(page, pageSize);

        List<Staff> staff1 = deptDao.selByDeptId(deptid, staff.getBranchid());
        PageInfo<Staff> pageInfo = new PageInfo<>(staff1);
        PageBean<Staff>pageBean = new PageBean<>(pageInfo.getTotal(),pageInfo.getList());
        return Result.success(pageBean);
    }
}
